package com.budwk.app.fetchdata.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.fetchdata.models.Dat_excelemergencyshelter;
import com.budwk.app.fetchdata.services.DatExcelemergencyshelterService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@IocBean
@At("/platform/fetchdata/excelemergencyshelter")
@ApiDefinition(tag = "应急避难数据表")
@Slf4j
public class DatExcelemergencyshelterController{
    @Inject
    private DatExcelemergencyshelterService datExcelemergencyshelterService;

    @Inject
    Dao dao;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询应急避难数据表")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = Dat_excelemergencyshelter.class
    )
    @ApiResponses(
    implementation = Dat_excelemergencyshelter.class
    )
    @SaCheckPermission("fetchdata.excelemergencyshelter")
    public Result<?> list(@Param("..")Dat_excelemergencyshelter bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getXzqy() !=null && Strings.isNotBlank(bean.getXzqy().toString())) {
            cnd.and("xzqy","like","%"+bean.getXzqy()+"%");
        }
        
        if (bean.getYjbncsqc() !=null && Strings.isNotBlank(bean.getYjbncsqc().toString())) {
            cnd.and("yjbncsqc","like","%"+bean.getYjbncsqc()+"%");
        }
        
        if (bean.getYjbncsdz() !=null && Strings.isNotBlank(bean.getYjbncsdz().toString())) {
            cnd.and("yjbncsdz","like","%"+bean.getYjbncsdz()+"%");
        }
        
        if (bean.getAtfsjlxfl() !=null && Strings.isNotBlank(bean.getAtfsjlxfl().toString())) {
            cnd.and("atfsjlxfl","like","%"+bean.getAtfsjlxfl()+"%");
        }
        
        if (bean.getBnzl() !=null && Strings.isNotBlank(bean.getBnzl().toString())) {
            cnd.and("bnzl","like","%"+bean.getBnzl()+"%");
        }
        
        if (bean.getAbnscsjfl() !=null && Strings.isNotBlank(bean.getAbnscsjfl().toString())) {
            cnd.and("abnscsjfl","like","%"+bean.getAbnscsjfl()+"%");
        }
        
        if (bean.getAkjlxfl() !=null && Strings.isNotBlank(bean.getAkjlxfl().toString())) {
            cnd.and("akjlxfl","like","%"+bean.getAkjlxfl()+"%");
        }
        
        if (bean.getAztgndwfl() !=null && Strings.isNotBlank(bean.getAztgndwfl().toString())) {
            cnd.and("aztgndwfl","like","%"+bean.getAztgndwfl()+"%");
        }
        
        if (bean.getYjbncsjslx() !=null && Strings.isNotBlank(bean.getYjbncsjslx().toString())) {
            cnd.and("yjbncsjslx","like","%"+bean.getYjbncsjslx()+"%");
        }
        
        if (bean.getYjbncszdzmj() !=null && Strings.isNotBlank(bean.getYjbncszdzmj().toString())) {
            cnd.and("yjbncszdzmj","like","%"+bean.getYjbncszdzmj()+"%");
        }
        
        if (bean.getYjbncssnmj() !=null && Strings.isNotBlank(bean.getYjbncssnmj().toString())) {
            cnd.and("yjbncssnmj","like","%"+bean.getYjbncssnmj()+"%");
        }
        
        if (bean.getBncsrlrs() !=null && Strings.isNotBlank(bean.getBncsrlrs().toString())) {
            cnd.and("bncsrlrs","like","%"+bean.getBncsrlrs()+"%");
        }
        
        if (bean.getWzcb() !=null && Strings.isNotBlank(bean.getWzcb().toString())) {
            cnd.and("wzcb","like","%"+bean.getWzcb()+"%");
        }
        
        if (bean.getYjss() !=null && Strings.isNotBlank(bean.getYjss().toString())) {
            cnd.and("yjss","like","%"+bean.getYjss()+"%");
        }
        
        if (bean.getRdbm() !=null && Strings.isNotBlank(bean.getRdbm().toString())) {
            cnd.and("rdbm","like","%"+bean.getRdbm()+"%");
        }
        
        if (bean.getZgdw() !=null && Strings.isNotBlank(bean.getZgdw().toString())) {
            cnd.and("zgdw","like","%"+bean.getZgdw()+"%");
        }
        
        if (bean.getJsdw() !=null && Strings.isNotBlank(bean.getJsdw().toString())) {
            cnd.and("jsdw","like","%"+bean.getJsdw()+"%");
        }
        
        if (bean.getJcsj() !=null && Strings.isNotBlank(bean.getJcsj().toString())) {
            cnd.and("jcsj","like","%"+bean.getJcsj().substring(0,10)+"%");

        }
        
        if (bean.getGhqk() !=null && Strings.isNotBlank(bean.getGhqk().toString())) {
            cnd.and("ghqk","like","%"+bean.getGhqk()+"%");
        }
        
        if (bean.getJsztz() !=null && Strings.isNotBlank(bean.getJsztz().toString())) {
            cnd.and("jsztz","like","%"+bean.getJsztz()+"%");
        }
        
        if (bean.getJsbzmc() !=null && Strings.isNotBlank(bean.getJsbzmc().toString())) {
            cnd.and("jsbzmc","like","%"+bean.getJsbzmc()+"%");
        }
        
        if (bean.getRcwhhglrysl() !=null && Strings.isNotBlank(bean.getRcwhhglrysl().toString())) {
            cnd.and("rcwhhglrysl","like","%"+bean.getRcwhhglrysl()+"%");
        }
        
        if (bean.getZzwhhglrysl() !=null && Strings.isNotBlank(bean.getZzwhhglrysl().toString())) {
            cnd.and("zzwhhglrysl","like","%"+bean.getZzwhhglrysl()+"%");
        }
        
        if (bean.getCqyydw() !=null && Strings.isNotBlank(bean.getCqyydw().toString())) {
            cnd.and("cqyydw","like","%"+bean.getCqyydw()+"%");
        }
        
        if (bean.getYwtr() !=null && Strings.isNotBlank(bean.getYwtr().toString())) {
            cnd.and("ywtr","like","%"+bean.getYwtr()+"%");
        }
        
        if (bean.getYwtrqd() !=null && Strings.isNotBlank(bean.getYwtrqd().toString())) {
            cnd.and("ywtrqd","like","%"+bean.getYwtrqd()+"%");
        }
        
        return Result.data(datExcelemergencyshelterService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增应急避难数据表")
    @ApiFormParams(
        implementation = Dat_excelemergencyshelter.class
    )
    @ApiResponses
    @SLog(tag = "新增应急避难数据表", value = "${args[0].id}")
    @SaCheckPermission("fetchdata.excelemergencyshelter.create")
    public Result<?> create(@Param("..")Dat_excelemergencyshelter datExcelemergencyshelter, HttpServletRequest req) {
        UUID idOne = UUID.randomUUID();
        String str=""+idOne;
        int uid=str.hashCode();
        String filterStr=""+uid;
        str=filterStr.replaceAll("-", "");
        Integer id=Integer.parseInt(str.substring(0,str.length()-2));
        datExcelemergencyshelter.setId(id);
        datExcelemergencyshelter.setCreatedBy(SecurityUtil.getUserId());
       /* String jcsj=datExcelemergencyshelter.getJcsj();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat();
        Date date=new Date();
        date.*/

        //datExcelemergencyshelter.setCreatedAt(Long.parseLong(new Date().toString()));
        //SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      /*  Date date = new Date();
        long times = date.getTime();
        Sql sql=Sqls.create("INSERT INTO datamanager.dat_excelemergencyshelter(id,xzqy,yjbncsqc,yjbncsdz,atfsjlxfl,bnzl,abnscsjfl,akjlxfl,aztgndwfl,yjbncsjslx,yjbncszdzmj,yjbncssnmj,bncsrlrs,wzcb,yjss,rdbm,zgdw,jsdw,jcsj,ghqk,jsztz,jsbzmc,rcwhhglrysl,zzwhhglrysl,cqyydw,ywtr,ywtrqd,createdBy,createdAt) VALUES" +
                "('"+Integer.parseInt(str)+"','"+datExcelemergencyshelter.getXzqy()+"','"+datExcelemergencyshelter.getYjbncsqc()+"','"+datExcelemergencyshelter.getYjbncsdz()+"','"+datExcelemergencyshelter.getAtfsjlxfl()+"','"+datExcelemergencyshelter.getBnzl()+"','"+datExcelemergencyshelter.getAbnscsjfl()+"'," +
                "'"+datExcelemergencyshelter.getAkjlxfl()+"','"+datExcelemergencyshelter.getAztgndwfl()+"','"+datExcelemergencyshelter.getYjbncsjslx()+"','"+datExcelemergencyshelter.getYjbncszdzmj()+"','"+datExcelemergencyshelter.getYjbncssnmj()+"','"+datExcelemergencyshelter.getBncsrlrs()+"','"+datExcelemergencyshelter.getWzcb()+"'," +
                "'"+datExcelemergencyshelter.getYjss()+"','"+datExcelemergencyshelter.getRdbm()+"','"+datExcelemergencyshelter.getZgdw()+"','"+datExcelemergencyshelter.getJsdw()+"','"+datExcelemergencyshelter.getJcsj()+"','"+datExcelemergencyshelter.getGhqk()+"','"+datExcelemergencyshelter.getJsztz()+"'," +
                "'"+datExcelemergencyshelter.getJsbzmc()+"','"+datExcelemergencyshelter.getRcwhhglrysl()+"','"+datExcelemergencyshelter.getZzwhhglrysl()+"','"+datExcelemergencyshelter.getCqyydw()+"','"+datExcelemergencyshelter.getYwtr()+"','"+datExcelemergencyshelter.getYwtrqd()+"','"+SecurityUtil.getUserId()+"','"+times+"')");
        dao.execute(sql);*/
        datExcelemergencyshelterService.insert(datExcelemergencyshelter);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST

    @ApiOperation(name = "修改应急避难数据表")
    @ApiFormParams(
        implementation = Dat_excelemergencyshelter.class
    )
    @ApiResponses
    @SLog(tag = "修改应急避难数据表", value = "${args[0].id}")
    @SaCheckPermission("fetchdata.excelemergencyshelter.update")
    public Result<?> update(@Param("..")Dat_excelemergencyshelter datExcelemergencyshelter, HttpServletRequest req) {
        datExcelemergencyshelter.setUpdatedBy(SecurityUtil.getUserId());
        datExcelemergencyshelterService.updateIgnoreNull(datExcelemergencyshelter);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取应急避难数据表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("fetchdata.excelemergencyshelter")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        Dat_excelemergencyshelter datExcelemergencyshelter = datExcelemergencyshelterService.fetch(Integer.parseInt(id));
        if (datExcelemergencyshelter == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(datExcelemergencyshelter);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除应急避难数据表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除应急避难数据表", value = "${args[0]}")
    @SaCheckPermission("fetchdata.excelemergencyshelter.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        Dat_excelemergencyshelter datExcelemergencyshelter = datExcelemergencyshelterService.fetch(Integer.parseInt(id));
        if (datExcelemergencyshelter == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        datExcelemergencyshelterService.delete(Integer.parseInt(id));
        req.setAttribute("_slog_msg", datExcelemergencyshelter.getId());
        return Result.success();
    }
}
